package com.hedl.practice.diyJava.gather.study_set.practice;

import com.hedl.practice.diyJava.gather.study_set.tmp.Message;

import java.util.*;

public class JavaAndPractice {
    public static void main(String[] args) {
        List<Message> received = Arrays.asList(
          new Message(1,"Hello!"),
                new Message(2,"发工资了吗？"),
                new Message(2,"发工资了吗？"),
                new Message(3,"去哪吃饭？"),
                new Message(3,"去哪吃饭？"),
                new Message(4,"Bye"),
                new Message(5,"不发工资了!"),
                new Message(6,"你发工资了吗？"),
                new Message(5,"在吗？"),
                new Message(7,"Apple")
        );

        List<Message> displayMessage = process(received);
        for (Message message : displayMessage) {
            System.out.println(message.text);
        }
    }

    /**
     * 在聊天软件中，发送方发送消息时，遇到网络超时后就会自动重发，
     * 因此，接收方可能会收到重复的消息，在显示给用户看的时候，需要首先去重。请练习使用Set去除重复的消息
     * @param received
     * @return
     */
    private static List<Message> process(List<Message> received) {
        Set<Message> set = new TreeSet<>(new Comparator<Message>(){
            public int compare(Message m1,Message m2){
                return m1.text.compareTo(m2.text);
            }
        });

        for(Message m:received){
            set.add(m);
        }
        return new ArrayList<Message>(set);
    }
}
